package com.listener.controller;

import com.listener.dao.MoveDao;
import com.listener.pojo.Move;
import com.listener.pojo.Page;
import com.listener.pojo.Pokemon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Controller
public class MoveController {
    @Autowired
    MoveDao moveDao;
    @RequestMapping("/selectMovePage")
    public String selectMovePage(Move moveCondition, Integer pageNum, HttpServletRequest request) throws IOException {
        if(pageNum==null){pageNum=1;}
        //如果条件不为空且不为""记录条件到session
        if((moveCondition.getType()!=null ||
                moveCondition.getMove()!=null||
                moveCondition.getCategory()!=null||
                moveCondition.getDescription()!=null)
        ){
            if (moveCondition.getType()!=null && moveCondition.getType().equals("")) moveCondition.setType(null);
            if (moveCondition.getMove()!=null && moveCondition.getMove().equals("")) moveCondition.setMove(null);
            if (moveCondition.getCategory()!=null && moveCondition.getCategory().equals("")) moveCondition.setCategory(null);
            if (moveCondition.getDescription()!=null && moveCondition.getDescription().equals("")) moveCondition.setDescription(null);
            request.getSession().setAttribute("moveCondition",moveCondition);
        }else {
            if(request.getSession().getAttribute("moveCondition")!=null){
                moveCondition= (Move) request.getSession().getAttribute("moveCondition");
            }
        }
        Page<Move> movePage =
                moveDao.selectMovePage(moveCondition,
                        pageNum,20);
        request.setAttribute("movePage",movePage);
        return "forward:/web/movePage.jsp";
    }
}
